﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class leibie_list : System.Web.UI.Page
{
// 2 请求参数成员变量列表
﻿    public string request_id = "";
﻿    public string request_mingcheng = "";


// 6.1 父列请求参数成员变量列表



    //初始化当前时间
    public string current_shijian = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Trim();
    //初始化当前日期
    public string current__riqi = DateTime.Now.ToString("yyyy-MM-dd").Trim();

    //查询数据以列表形式显示
    protected void Page_Load(object sender, EventArgs e)
    {
        //初始化每页数量
        DataGrid1.PageSize = new Class1().GetPageSize();

//每次执行
//4 初始化参数列表 获取请求参数
﻿        if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString().Trim() != "")
        {
             request_id = Request.QueryString["id"];
        }
        
﻿        if (Request.QueryString["mingcheng"] != null && Request.QueryString["mingcheng"].ToString().Trim() != "")
        {
             request_mingcheng = Request.QueryString["mingcheng"];
        }
        


        string sql;
        sql = "select * from leibie where 1=1 ";


//5 根据获取请求参数组织sql语句
﻿
        if (request_id != null && request_id.ToString().Trim() != "")
        {
            sql = sql + " and id = '" + request_id.ToString().Trim() + "'";
        }
﻿
        if (request_mingcheng != null && request_mingcheng.ToString().Trim() != "")
        {
            sql = sql + " and mingcheng = '" + request_mingcheng.ToString().Trim() + "'";
        }



        sql = sql + " order by id desc";

        //第一次访问,非回发时执行！
        if (!IsPostBack)
        {
//3 初始化参数列表




            getdata(sql);
            //Response.Write("第一次访问,非回发时执行！");
        }
        else
        {
            //Response.Write("第一次以后,回发");
        }
    }



    //查询数据库到DataSet，然后绑定数据显示到页面
    private void getdata(string sql)
    {
        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);
        if (dsresult != null)
        {

            if (dsresult.Tables[0].Rows.Count > 0)
            {
                if (dsresult.Tables[0].Rows.Count > 0)
                {

// 6.2 向数据集中添加父列



                    for (int i = 0; i < dsresult.Tables[0].Rows.Count; i++)
                    {
// 6.3 循环向数据集中添加的父列赋值



                    }
                }

                //绑定数据
                DataGrid1.DataSource = dsresult.Tables[0];
                DataGrid1.DataBind();
                Label1.Text = "以上数据中共" + dsresult.Tables[0].Rows.Count + "条";

            }
            else
            {
                DataGrid1.DataSource = null;
                DataGrid1.DataBind();
                Label1.Text = "暂无任何数据";
            }
        }
    }

    //查询功能
    protected void Search_Click(object sender, EventArgs e)
    {
        string sql;
        sql = "select * from leibie where 1=1";

//8 组织sql语句的属性与值
﻿
        if (IsExistControl("id") && GetControl("id").Text.ToString().Trim() != "")
        {
            sql = sql + " and id like '%" + GetControl("id").Text.ToString().Trim() + "%'";
        }
﻿
        if (IsExistControl("mingcheng") && GetControl("mingcheng").Text.ToString().Trim() != "")
        {
            sql = sql + " and mingcheng like '%" + GetControl("mingcheng").Text.ToString().Trim() + "%'";
        }


        sql = sql + " order by id desc";

        getdata(sql);
    }

    protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)//分页功能
    {
        string sql;
        sql = "select * from leibie order by id desc";
        getdata(sql);
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        DataGrid1.DataBind();
    }

    public string riqigeshi(object str)//获取日期格式
    {
        string strTmp = str.ToString();
        DateTime dt = Convert.ToDateTime(strTmp);
        string ss = dt.ToShortDateString();
        return ss;

    }

    protected void Export_Click(object sender, EventArgs e)//导出Excel
    {
        string sql;
        sql = "select * from leibie order by id desc";

        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);

        DataGrid1.PageSize = dsresult.Tables[0].Rows.Count + 1;
        getdata(sql);

        new Class1().ToExcel(DataGrid1, "leibie");
    }



    //选择父类信息
    private void addselect(string FatherTable, string FatherIdColumnName, string FatherSecondColumnName, string DropDownListControlId)
    {
        //控件不存在则返回
        if (this.FindControl(DropDownListControlId) == null)
        {
            //Response.Write("控件未找到!!!   " + DropDownListControlId);
            return;
        }

        string sql;
        sql = "select " + FatherIdColumnName + "," + FatherSecondColumnName + " from " + FatherTable + " order by id desc";
        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);
        if (dsresult != null)
        {
            if (dsresult.Tables[0].Rows.Count > 0)
            {

                ListItem it0 = new ListItem();
                it0.Value = "";
                it0.Text = "全部";
                ((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it0);
                for (int i = 0; i < dsresult.Tables[0].Rows.Count; i++)
                {
                    ListItem it = new ListItem();
                    it.Value = dsresult.Tables[0].Rows[i][0].ToString().Trim();
                    it.Text = dsresult.Tables[0].Rows[i][1].ToString().Trim();
                    ((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it);
                }

            }
        }
    }

    //通过当前信息id作为父类id选择父类
    private void addselectbyId(string FatherTable, string FatherIdColumnName, string FatherSecondColumnName, string DropDownListControlId, string FatherIdColumnValue)
    {
        //控件不存在则返回
        if (this.FindControl(DropDownListControlId) == null)
        {
            //Response.Write("控件未找到!!!   " + DropDownListControlId);
            return;
        }

        string sql;
        sql = "select " + FatherIdColumnName + "," + FatherSecondColumnName + " from " + FatherTable + " where " + FatherIdColumnName + "='" + FatherIdColumnValue + "'" + " order by id desc";
        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);
        if (dsresult != null)
        {
            if (dsresult.Tables[0].Rows.Count > 0)
            {

                ListItem it0 = new ListItem();
                it0.Value = "";
                it0.Text = "全部";
                ((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it0);
                for (int i = 0; i < dsresult.Tables[0].Rows.Count; i++)
                {

                    ListItem it = new ListItem();
                    it.Value = dsresult.Tables[0].Rows[i][0].ToString().Trim();
                    it.Text = dsresult.Tables[0].Rows[i][1].ToString().Trim();
                    ((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it);
                }

            }
        }
    }

    //获取父类信息
    private string getFatherSecondColumnValue(string FatherTable, string FatherSecondColumnName, string FatherIdColumnVaule)
    {
        string FatherSecondColumnValue = "";
        string sql;
        sql = "select " + FatherSecondColumnName + " from " + FatherTable + " where 1=1 and id='" + FatherIdColumnVaule + "' order by id desc";

        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);
        if (dsresult != null)
        {
            if (dsresult.Tables[0].Rows.Count > 0)
            {
                FatherSecondColumnValue = dsresult.Tables[0].Rows[0][0].ToString().Trim();
            }
        }

        return FatherSecondColumnValue;
    }

    public void IsEmptyControl(string ControlId)//判断控件内容是否为空
    {
        if (!IsExistControl(ControlId) || new Class1().IsEmpty(GetControl(ControlId).Text.ToString().Trim()))
        {
            Response.Write("<script>javascript:alert('输入不能为空,请重新输入!');history.back();</script>");
            Response.End();
        }
    }

    public void IsHandset(string ControlId)//判断控件内容是否手机号
    {
        IsEmptyControl(ControlId);
        if (!new Class1().IsHandset(GetControl(ControlId).Text.ToString().Trim()))
        {
            Response.Write("<script>javascript:alert('输入格式不正确,请重新输入手机号格式!');history.back();</script>");
            Response.End();
        }
    }

    public void IsEmail(string ControlId)//判断控件内容是否邮箱
    {
        IsEmptyControl(ControlId);
        if (!new Class1().IsEmail(GetControl(ControlId).Text.ToString().Trim()))
        {
            Response.Write("<script>javascript:alert('输入格式不正确,请重新输入邮箱格式!');history.back();</script>");
            Response.End();
        }
    }

    public void IsIDcard(string ControlId)//判断控件内容是否身份证号
    {
        IsEmptyControl(ControlId);
        if (!new Class1().IsIDcard(GetControl(ControlId).Text.ToString().Trim()))
        {
            Response.Write("<script>javascript:alert('输入格式不正确,请重新输入身份证格式!');history.back();</script>");
            Response.End();
        }
    }

    public bool IsExistControl(string DropDownListControlId)  //判断控件是否存在
    {
        if (this.FindControl(DropDownListControlId) != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public ITextControl GetControl(string ControlId)  //获取ITextControl控件对象
    {
        if (this.FindControl(ControlId) != null)
        {
            return (ITextControl)this.FindControl(ControlId);
        }
        else
        {
            return null;
        }
    }

    public TextBox GetTextBoxControl(string TextBoxControlId)  //获取TextBox控件对象
    {
        if (this.FindControl(TextBoxControlId) != null)
        {
            return (TextBox)this.FindControl(TextBoxControlId);
        }
        else
        {
            return null;
        }
    }

    public DropDownList GetDropDownListControl(string DropDownListControlId)  //获取DropDownList控件对象
    {
        if (this.FindControl(DropDownListControlId) != null)
        {
            return (DropDownList)this.FindControl(DropDownListControlId);
        }
        else
        {
            return null;
        }
    }
}

